<%@ page import="com.ld.businesscard_manage.service.BusinessCardService" %>
<%@ page import="com.ld.businesscard_manage.service.impl.BusinessCardServiceImpl" %>
<%@ page import="org.apache.commons.codec.digest.DigestUtils" %>
<%@ page import="com.ld.businesscard_manage.bean.BusinessCard" %>
<%@ page import="com.ld.businesscard_manage.service.UserService" %>
<%@ page import="com.ld.businesscard_manage.service.impl.UserServiceImpl" %>
<%@ page import="com.ld.businesscard_manage.bean.User" %><%--
  Created by IntelliJ IDEA.
  User: 刘邓
  Date: 2023-06-10
  Time: 9:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加名片结果</title>
</head>
<body>
<%!
  private BusinessCardService cardService = new BusinessCardServiceImpl();
  private UserService userService = new UserServiceImpl();
%>
<%
  String name = request.getParameter("name");
  String sex0 = request.getParameter("sex");
  String account = request.getParameter("account");
  String password0 = request.getParameter("password");
  String repassword = request.getParameter("repassword");
  String email = request.getParameter("email");
  if (name == "") response.sendRedirect("/home/addCard.jsp");
  else if (sex0 == "") response.sendRedirect("/home/addCard.jsp");
  else if (account == "") response.sendRedirect("/home/addCard.jsp");
  else if (password0 == "") response.sendRedirect("/home/addCard.jsp");
  else if (repassword == "") response.sendRedirect("/home/addCard.jsp");
  else if (email == "") response.sendRedirect("/home/addCard.jsp");
  else if(!password0.equals(repassword)) response.sendRedirect("/home/addCard.jsp");
  else {
    Integer sex = Integer.parseInt(sex0);
    String password = DigestUtils.md5Hex(password0);
    BusinessCard card = new BusinessCard();
    card.setName(name);
    card.setSex(sex);
    card.setAccount(account);
    card.setPassword(password);
    card.setEmail(email);
    card.setValid(0);
    User userByName = userService.getUserByName(account);
    if (userByName != null) {
      if(userByName.getPassword().equals(card.getPassword())){
        int i = cardService.addCard(card);
        if(i == 0){
          out.println("<h1>添加失败</h1>");
        }else {
          out.println("<h1>添加成功</h1>");
        }
      }else {
        out.println("<h1>添加的账号已经存在，但密码不对</h1>");
      }
    } else {
      User user = new User();
      user.setUsername(account);
      user.setPassword(password);
      user.setStatus(0);
      int j = userService.addUser(user);
      int i = cardService.addCard(card);
      if (i == 0 || j == 0) {
        out.println("<h1>添加失败</h1>");
      } else {
        out.println("<h1>添加成功</h1>");
      }
    }
  }
  out.println("<a href='/home/manageCard.jsp'>返回名片管理页面</a>");
%>
</body>
</html>
